package model.tokens;

import java.util.Stack;
import java.util.Queue;

import model.semantics.Node;

/**
 * Operators are things like +, *, /, -.
 */

public class OperatorToken<T> extends FunctionalToken<T> {
	public OperatorToken(String symbol, String syntaxFileName) {
		super(symbol, syntaxFileName);
	}
	
	/**
	 * Handle the processing of operators in the Shunting-Yard algorithm.
	 * @param operatorStack the stack of operator tokens.
	 * @param output the queue of tokens output by the algorithm.
	 */
	@Override
	public void process(Stack<Token<T>> operatorStack, Stack<Node<T>> output) {
		//shuntFunctionsToOutput(operatorStack, output);
		
		/* While there is an operator on the stack with higher precedence than this token,
		shunt it to the output queue, then add the token to the stack. */
		while(!operatorStack.empty() && operatorStack.peek() instanceof OperatorToken
				&& ((OperatorToken<?>) operatorStack.peek()).getPrecedence() >= getPrecedence()) {
			output.add(((OperatorToken<T>) operatorStack.pop()).makeNewInstance(output));
		}
		operatorStack.push(this);
	}

}
